# -*- coding : utf-8 -*-

def index():
    sip_dict = session.sip_dict

#    #login keys and login url
#    sip_login_url = taobao_api.sip_url(login_keys_list, sip_dict)
#    sip_login_url = SIPLOGINURL + '?' + sip_login_url
#
#    #start keys and start url
#    sip_start_sign = taobao_api.sip_sign(start_keys_list, sip_dict)
#    sip_start_url = taobao_api.sip_url(start_keys_list, sip_dict)
#    sip_start_url = SIPRESTURL + '?' + sip_start_url + 'sip_sign=' + sip_start_sign
#
#    #commmon url
#    sip_common_url = taobao_api.sip_url(common_keys_list, sip_dict)
#    sip_common_sign = taobao_api.sip_sign(common_keys_list, sip_dict)
#    sip_common_url = SIPRESTURL + '?' + sip_common_url + 'sip_sign=' + sip_common_sign
    sip_start_url = taobao_api.get_url(start_keys_list, sip_dict, sip_sign=True)

    fetch_start = fetch(sip_start_url)
#    fetch_common = fetch(sip_common_url)
    try:
        from google.appengine.api import urlfetch
        gae_result_start = urlfetch.fetch(sip_start_url).content
#        gae_result_common = urlfetch.fetch(sip_common_url).content
    except:
        gae_result = gae_result_start = gae_result_common = 'zzz'

    #sesponse start
    response.menu = [['首页', False,URL(r=request, a='welcome', c='default', f='index')], ['淘宝易', True,URL(r=request, f='index')]]

    return dict(
#        sip_dict = sip_dict,
        sip_start_url = sip_start_url,
#        sip_start_url_type = type(sip_start_url),
#        sip_login_url = sip_login_url,
#        sip_common_url = sip_common_url,
        fetch_start = fetch_start,
#        fetch_common = fetch_common,
        gae_result_start = gae_result_start,
#        gae_result_common = gae_result_common,
#        url = url,
#        payload = payload,
        sip_dict_type = type(sip_dict),
        )

def sip():
    config_name = 'sip'
    sip_config = ['appkey', 'secret', 'url', 'v']
    query = (db.config.name == config_name)

    form = db().select(db.config.ALL)
#    config = db(query).select('appkey', 'v', 'secret', 'url')
    config = db(query).select()
    config = config[0]

    response.flash = request.vars.flash

    return dict(
        form = SQLTABLE(form),
        config = config
        )

def interface():
    each_api = [i.api for i in db().select(db.interface.ALL)]
    form = FORM(TABLE(
        TR("API:",SELECT(each_api, _name='api', requires=IS_IN_SET(each_api))),
        TR("",INPUT(_type="submit",_value="SUBMIT")),
        ))
    if form.accepts(request.vars,session):
        response.flash="form accepted"
    elif form.errors:
        response.flash="form is invalid"

    try:
        api_name = form.vars.api
    except:
        api_name = None
    table = db(db.interface.api==api_name).select()

    return dict(
        table = table,
        each_api = each_api,
        form = form,
        )

def test():
    form = [i.api for i in db().select(db.interface.ALL)]
    table = SQLTABLE(db.interface.ALL)
    return dict(
        form = form,
        )